<?php
/**
 * @file forumreplayaction.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015-9-24 下午4:05:49 $
 * @brief  $
 */
namespace haibao\cms\data;

class ForumReplayAction extends \haibao\cms\data\BaseMysql{
	
	public function __construct(){
		parent::__construct('\haibao\cms\model\data\ForumReplayAction');
	}
	
	/**
	 * 获取已回复数
	 */
	public function getRestCount($forumReplayIdArr){
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$forumIdStr = preg_replace('/\d+/','%s',implode(',',$forumReplayIdArr));
		$sql = "select tid,count(*) as count from hb_forum_post where tid in (".$forumIdStr.") and useip = '' group by tid";
		$forumResult = $this->query($sql,$forumReplayIdArr);
		$forumArr = array();
		while($row = $forumResult->fetch_assoc()){
			$forumArr[$row['tid']] = $row['count'];
		}
		return $forumArr;
	}
	
	/**
	 * 更改帖子回复状态（关闭）
	 */
	public function closeForumReplay($forumIdArr){
		$forumIdStr = preg_replace('/\d+/','%s',implode(',',$forumIdArr));
		$sql = "update ForumReplayAction set Status = 2 where ForumId in (".$forumIdStr.")";
		$this->query($sql,$forumIdArr);
	}
	
	/**
	 * 添加批量回复帖子操作
	 * @param unknown $model
	 */
	public function saveForumReplayAction($model){
		 $this->add($model);
	}
	
	/**
	 * 检查帖子ID是否存在
	 * @param unknown $forumId
	 */
	public function checkForumId($forumId){
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$sql = "select tid from hb_forum_post where tid = %s";
		$result = $this->query($sql,array($forumId))->fetch_assoc();
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_CMS );
		return $result;
	}
	
	/**
	 * 删除帖子回复
	 */
	public function deleteReplay($delForumId,$forumFloorOne,$forumFloorTwo){
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$delsql = "delete from hb_forum_post where tid = %s and position between %s and %s";
		$this->query($delsql,array($delForumId,$forumFloorOne,$forumFloorTwo));
		
		$csql = "select max(position) as maxposition,count(*) as countnum from hb_forum_post where tid = %s";
		$forumPositionResult = $this->query($csql,array($delForumId))->fetch_assoc();
		
		if($forumPositionResult){
			$usql = "update hb_forum_thread set maxposition = %s,replies = %s where tid = %s";
			$this->query($usql,array($forumPositionResult['maxposition'],$forumPositionResult['countnum']-1,$delForumId));
		}
		
		$memcache = new \haibao\frame\data\MemcacheHelper();
		$servers = \haibao\classlibrary\cms\Config::getConfig(\haibao\frame\Environment::CONFIG_MEMCACHE, \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MEMCACHE_BBS);
		$config = new \haibao\frame\data\config\MemcacheConfig($servers);
		$memcache->setConfig($config);
		$key = "cNWoya_forum_thread_".$delForumId;
		$memcache->delValue($key);
	}
	
	/**
	 * 修改帖子浏览数
	 */
	public function editViewNum($model){
		$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
		$sql = "update hb_forum_thread set views=%s where tid=%s limit 1";
		$this->query($sql,array(
				$model->ViewNum, $model->ForumId
		));
		
		$memcache = new \haibao\frame\data\MemcacheHelper();
		$servers = \haibao\classlibrary\cms\Config::getConfig(\haibao\frame\Environment::CONFIG_MEMCACHE, \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MEMCACHE_BBS);
		$config = new \haibao\frame\data\config\MemcacheConfig($servers);
		$memcache->setConfig($config);
		$key = "cNWoya_forum_thread_".$model->ForumId;
		$memcache->delValue($key);
	}
}